var app = new Vue({
    el:'#app',
    data:{
        newLogin:'',
        //给所在地赋值
        region:returnCitySN.cname,
        account:'',
        deptName:'',
        lastLogin: '',

        productOrderCounts: '',
        courseOrderCounts:'',
        alreadyAccomplishCounts: '',
        toAuditProduct:'',
        toAuditMicroRing:'',
        toAuditCourse:'',
        expireDate: ''
    },
    created(){
        var self = this;
        var allMessageCount = 0;
        var a = $("input[name=user]").val();
        var c = a.substring(a.indexOf('(')+1,a.indexOf(')')).split(', ');
        var d = {
            data:{}
        };

        for (var i = 0; i < c.length; i++) {
            var n = c[i].split('=');
            d.data[n[0]] = n[1];
        }

        localStorage.setItem("user",JSON.stringify(d));
        //给当前用户名字赋值
        var user = JSON.parse(localStorage.getItem('user'));
        this.account = user.data.account;

        $("#imgSrc").attr("src",user.data.acatarUrl);
        //给当前用户所在部门赋值
        $.ajax({
            url: '/SyDept/getSyJsDept',
            data: {'account':user.data.userId},
            dataType: 'json',
            type: 'post',
            success:function (data) {
                /*self.deptName = data.data.deptName;*/
                var deptName = '';
                for (var i = 0; i < data.data.length; i++) {
                    if(data.data.length <= 1){
                        deptName = data.data[i].deptName;
                    }else{
                        if(data.data.length - i != 1){
                            deptName += data.data[i].deptName + '、';
                        }else{
                            deptName += data.data[i].deptName;
                        }
                    }
                }
                self.deptName = deptName;
            }
        });
        //给上次登录时间赋值
        $.ajax({
            url: '/loginRecord/getLastLogin',
            data: {'account':user.data.userId},
            dataType: 'json',
            type: 'post',
            success:function (data){
                self.lastLogin = data.data;
            }
        });
        //获取 年月日 时分秒 星期
        function setTime(){
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var today = date.getDate();
            var week = date.getDay();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            var weekArr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
            //当小时小于10时进行补零
            var currentTime = zeroFill(year) + '-' + zeroFill(month) + '-' + zeroFill(today) +  '                   ' + zeroFill(hour) + ':' + zeroFill(minute) + ':' + zeroFill(second) + '                    ' + weekArr[week];
            return currentTime;
        }
        //补零
        function zeroFill(time){
            if(time < 10){
                time = '0' + time;
            }
            return time;
        }
        //给当前登录时间赋值
        this.newLogin = setTime();
        //查询待审核商品、课程、微圈
        getToAudits(2);
        getToAudits(3);
        getToAudits(5);
        getProductCountsByStatusCreateId(2);
        getProductCountsByStatusCreateId(3);
        function getToAudits(type){
            $.ajax({
                url: '/SyCourseRecord/getToAuditsCount',
                dataType: 'json',
                data: {
                    result: 2,
                    type: type
                },
                async: false,
                success: function (data) {
                    if(type == 2){
                        if(data.data == 0){
                            $('#toAuditCourse').hide();
                        }
                        self.toAuditCourse = data.data;
                    }else if(type == 3){
                        if(data.data == 0){
                            $('#toAuditProduct').hide();
                        }
                        self.toAuditProduct = data.data;
                    }else if(type == 5){
                        if(data.data == 0){
                            $('#toAuditMicroRing').hide();
                        }
                        self.toAuditMicroRing = data.data;
                    }
                }
            });
        }
        //查询不同状态订单数量
        function getProductCountsByStatusCreateId(orderType){
            $.ajax({
                url: '/SyUserOrder/getProductCountsByStatusCreateId',
                data: {createId: user.data.userId,orderType: orderType},
                dataType: 'json',
                type: 'post',
                async: false,
                success: function (data) {
                    if(orderType == 2){
                        if(data.data == 0){
                            $('#courseOrder').hide();
                        }
                        self.courseOrderCounts = data.data;
                    }else if(orderType == 3){
                        if(data.data == 0){
                            $('#productOrder').hide();
                        }
                        self.productOrderCounts = data.data;
                    }
                }
            });
        }
        //查询当前用户广告即将到期条数
        $.ajax({
            url: '/JsCarousel/getAdvertisingCountsByCreateId',
            data:{
                createId: user.data.userId
            },
            dataType: 'json',
            async: false,
            success: function (data) {
                self.expireDate = data.data;
                if(data.data == 0){
                    $('#slideshowList').hide();
                }
            }
        })
        /*var message = $('#messageAlert div');
        var len = $(message).length;
        $.each(message,function (i, ele) {
            if($(ele).css('display') != 'none'){

            }
        })*/
        //注意：这行代码始终在这个作用域的最后一行
        $('#allMessageCount').text(parseInt(self.expireDate) + parseInt(self.courseOrderCounts) + parseInt(self.productOrderCounts) + parseInt(self.toAuditMicroRing) + parseInt(self.toAuditProduct) + parseInt(self.toAuditCourse));
    },
    beforeMount(){
        //跳转页面然后添加登录日志
        var ip = returnCitySN.cip;//本地ip
        var address = returnCitySN.cname;//本地区名
        var user = JSON.parse(localStorage.getItem('user'));

        //现在时间
        function getTime(){
            var date=new Date();
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            var day=date.getDate();
            var hour=date.getHours();
            var minute=date.getMinutes();
            var second=date.getSeconds();
            var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+"   "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
            return dateStr;
        }
        //补零
        function zeroFilling(data){
            if(data<10){
                data="0"+data;
            }
            return data;
        }

    },
    methods:{
        //头部右边导航栏弹框切换效果
        personalInformation(){
            $('#personalInformation').css('display','inline-block');
            $('#inCommonUseMenu').css('display', 'none');
            $('#messageAlert').css('display', 'none');
        },
        hiddenPersonalInformation() {
            $('#personalInformation').css('display', 'none');
        },
        inCommonUseMenu(){
            $('#personalInformation').css('display', 'none');
            $('#inCommonUseMenu').css('display','inline-block');
            $('#messageAlert').css('display', 'none');
        },
        hiddenInCommonUseMenu(){
            $('#inCommonUseMenu').css('display', 'none');
        },
        messageAlert(){
            $('#personalInformation').css('display', 'none');
            $('#messageAlert').css('display','inline-block');
            $('#inCommonUseMenu').css('display', 'none');
        },
        hiddenMessageAlert(){
            $('#messageAlert').css('display', 'none');
        },
        hiddenAll(){
            $('#personalInformation').css('display', 'none');
            $('#messageAlert').css('display', 'none');
            $('#inCommonUseMenu').css('display', 'none');
        },
    }
});
var element, layer, util, $,tabrightmenu;
layui.config({
    base:'/layui-down/'
}).use(['element', 'layer', 'util','jquery','tabrightmenu'], function(){
    element = layui.element,
        layer = layui.layer,
        util = layui.util,
        $ = layui.jquery,
        tabrightmenu = layui.tabrightmenu;

    tabrightmenu.render({
        container: '#nav-tab',
        filter: 'tab',
        navArr: [
            {eventName: 'closethis', title: '关闭当前'},
            {eventName: 'closeall', title: '关闭所有'},
            {eventName: 'closeothers', title: '关闭其它'},
        ],
    });
    var user=JSON.parse(localStorage.getItem("user"));

    getNav();
    tabIndexCloseDisplay();
    //退出账户
    $('#quitAccount').click(function () {
        localStorage.removeItem("user");
        $.ajax({
            url: '/login/logout',
            type: 'post',
        });
    })


    function getNav() {
        var src;
        var iframes;
        $.ajax({
            url: '/jsMenu/getJsMenus',
            data: {'userId':user.data.userId},
            method:'post',
            dataType:'json',
            success:function (data) {
                var nav = data.data;
                var html = '';
                html +='<ul class="layui-nav layui-nav-tree"  lay-filter="nav-tree">';
                $.each(nav,function (i,item) {
                    html = html+'<li class="layui-nav-item">';
                    html += '<a href="javascript:;">'+item.menuName+'</a>';
                    if(item.children.length > 0){
                        html += '<dl class="layui-nav-child">';
                        $.each(item.children,function (j,children) {
                            html+='<dd>';
                            if(children.url != null  && children.url != '' && children.url != 'null' && children.url != 'Null' && children.url != 'NULL' ){
                                html += '<a href="javascript:;" class="subMenu" _href="'+children.url+'" >'+children.menuName+'</a>';
                            }else {
                                html += '<a href="javascript:;">'+children.menuName+'</a>';
                            }
                            if(children.children.length > 0){
                                html += '<dl class="layui-nav-child">';
                                $.each(children.children,function (k,childrenItem) {
                                    html += '<dd>'+
                                        '<a href="javascript:;" class="subMenu" _href="'+childrenItem.url+'">'+childrenItem.menuName+'</a>'+
                                        '</dd>';
                                });
                                html+= '</dl>';
                            }
                        });
                        html+= '</dl>' ;
                    }
                    html+= '</li>';
                });
                html+='</ul>';
                //将拼接完成的字符串进行添加
                $("#table1").append(html);
                //重载导航栏
                element.init();

                //进行系统首页的选择，固定代码
                $('.subMenu').each(function () {
                    if($(this).text() == '首页'){
                        $('li[lay-id=0] ').children('i').each(function () {
                            if($(this).hasClass('layui-icon-close')){
                                $(this).hide();
                            }
                        });
                        $(this).click();
                        $('.layui-nav-tree').children().children().first().click();
                        $('.layui-nav-tree').children().children('dl').children().children().first().click();
                    }
                });

                //一级目录的手风琴
                $('.layui-nav-item').click(function () {
                    $(this).siblings().removeClass('layui-nav-itemed');
                })

                //二级目录的手风琴
                $('.layui-nav-item').children('dl').children('dd').click(function () {
                    $(this).siblings().removeClass('layui-nav-itemed');
                })
                //进行选项卡的添加和去重
                $('.subMenu').click(function () {
                    var href = $(this).attr('_href');
                    var title = $(this).text();
                    var li;
                    var len
                    iframes = [];
                    $('.iframe').each(function (i) {
                        iframes[i] = $(this).attr('src');
                    });
                    for (var j = 0; j < iframes.length; j++) {
                        if (iframes[j] == href){
                            li = 'li[lay-id='+j+']';
                            $(li).click();
                            return;
                        }
                    }
                    if ($('.iframe').length == 1){
                        src = '首页';
                    }
                    li = '';
                    len =  $('.layui-tab-title li').length;

                    var i = new Date().getTime();

                    if(title.indexOf('广告位即将到期') != -1){
                        title = '轮播图列表';
                    }else if(title.indexOf('待审核商品') != -1){
                        title = '商品审核列表';
                    }else if(title.indexOf('待审核课程') != -1){
                        title = '课程审核列表';
                    }else if(title.indexOf('待审核微圈') != -1){
                        title = '微圈审核列表';
                    }else if(title.indexOf('商品订单') != -1){
                        title = '商品订单列表';
                    }else if(title.indexOf('课程订单') != -1){
                        title = '课程订单列表';
                    }
                    element.tabAdd('tab',{
                        title:title,
                        content:'<iframe  quickEntryIndex="首页" scrolling="yes" frameborder="0"  src="'+href+'"  class="iframe" _href="'+title+'" before_href="'+src+'"></iframe>',
                        id:len
                    });
                    li = 'li[lay-id='+len+']';
                    $(li).click();
                    if($('.iframe').length > 1) {
                        src = title;
                    }

                });

                //返回后跳转上一个页面
                beforeHref = function beforeHref() {
                    $('.iframe').each(function () {
                        if($(this).attr('_href') == $('#beforeHref').val()){
                            var beforeHref = $(this).attr('before_href');
                            $('.subMenu').each(function () {
                                if($(this).text() == beforeHref){
                                    $(this).click();
                                    return;
                                }
                            })
                        }
                    })
                }
                quickEntryIndex = function quickEntryIndex(){
                    $('.iframe').each(function () {
                        if($(this).attr('quickEntryIndex') == $('#quickEntryIndex').val()){
                            var quickEntryIndex = $(this).attr('quickEntryIndex');
                            $('.subMenu').each(function () {
                                if($(this).text() == quickEntryIndex){
                                    $(this).click();
                                    return false;
                                }
                            })
                        }
                    })
                }
                //快捷入口添加选项卡跳转页面
                quickEntry = function quickEntry(){
                    var quickEntry = $('#quickEntry').val();
                    $('.subMenu').each(function () {
                        if($(this).text() == quickEntry){
                            $(this).click();
                        }
                    })
                }
            }
        })
    }
    function tabIndexCloseDisplay() {
        if($('#tab .layui-this').attr('lay-id') == 0){
            $('#tab .layui-this').children('.layui-tab-close').css('display','none');
        }
    }
});
